package com.mikao.service.impl;

import com.mikao.dao.SysRoleDao;
import com.mikao.dao.SysRolePermissionDao;
import com.mikao.dao.SysUserDao;
import com.mikao.dto.bo.ManagerForm;
import com.mikao.dto.bo.ReqDataTableDto;
import com.mikao.dto.vo.RspDataTableDto;
import com.mikao.dto.vo.SysManagerDto;
import com.mikao.dto.vo.SysUserDto;
import com.mikao.entity.SysRole;
import com.mikao.entity.SysRolePermission;
import com.mikao.entity.SysUser;
import com.mikao.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @author KennyDavid
 */

@Service
public class SysUserServiceImpl implements SysUserService {

    private final SysUserDao sysUserDao;
    private final SysRoleDao sysRoleDao;
    private final SysRolePermissionDao sysRolePermissionDao;

    public SysUserServiceImpl(SysUserDao sysUserDao, SysRoleDao sysRoleDao, SysRolePermissionDao sysRolePermissionDao) {
        this.sysUserDao = sysUserDao;
        this.sysRoleDao = sysRoleDao;
        this.sysRolePermissionDao = sysRolePermissionDao;
    }

    @Override
    public SysUserDto findByAccount(String account) {
        //系统对象
        SysUser sysUser = sysUserDao.findByAccountAndStatus(account, true);
        //系统角色
        SysRole sysRole = sysRoleDao.findOne(sysUser.getRoleId());
        //系统权限
        SysRolePermission sysRolePermission = sysRolePermissionDao.findByRoleId(sysRole.getId());
        String[] permissionIds = sysRolePermission.getPermissionIds().split(",");
        SysUserDto dto = new SysUserDto();

        BeanUtils.copyProperties(sysUser, dto);
        dto.setRoleName(sysRole.getRoleName());
        dto.setPermissionIds(permissionIds);
        return dto;
    }

    @Override
    public RspDataTableDto<SysManagerDto> findManagerList(ReqDataTableDto dataTableDTO) {
        Page<SysUser> page = sysUserDao.findAll(new PageRequest(dataTableDTO.getPage(), dataTableDTO.getLength(),
                new Sort(Sort.Direction.DESC, "createTime")));

        List<SysManagerDto> dtos = new ArrayList<>();
        SysManagerDto managerDto;
        for (SysUser sysUser : page.getContent()) {
            managerDto = new SysManagerDto();
            BeanUtils.copyProperties(sysUser, managerDto);
            //根据用户ID得到对应角色名
            managerDto.setRoleName(sysRoleDao.findOne(sysUser.getRoleId()).getRoleName());
            dtos.add(managerDto);
        }
        return new RspDataTableDto<>(dataTableDTO.getDraw(), page.getTotalElements(), dtos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveManager(ManagerForm managerForm) {
        SysUser sysUser;
        if (managerForm.getId() != null) {
            sysUser = sysUserDao.findOne(managerForm.getId());
        } else {
            sysUser = new SysUser();
            sysUser.setAccount(managerForm.getAccount());
            Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();
            String password = md5PasswordEncoder.encodePassword(managerForm.getPassword(), managerForm.getAccount());
            sysUser.setPassword(password);
        }
        if (managerForm.getStatus() == null) {
            sysUser.setStatus(false);
        } else {
            sysUser.setStatus(true);
        }
        sysUser.setNickname(managerForm.getNickname());
        sysUser.setRoleId(managerForm.getRoleId());

        sysUserDao.saveAndFlush(sysUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeManager(Long id) {
        sysUserDao.delete(id);
    }

    @Override
    public boolean checkSysUserExists(String account) {
        return sysUserDao.findByAccount(account) == null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resetPassword(Long id) {
        SysUser sysUser = sysUserDao.findOne(id);
        sysUser.setPassword(new Md5PasswordEncoder().encodePassword("123456", sysUser.getAccount()));
        sysUserDao.saveAndFlush(sysUser);
    }
}
